import {defineStore} from 'pinia'
import cache from "@/units/cache"

export type GuardType = {
    account: string
    avatar: string
    id: number
    nickname: string
    power: Array<string>
    role: Array<string>
    token: string
}

/**
 * 后台菜单
 */
export default defineStore('app-guard', {
    state(): GuardType {
        return {
            id: 0,
            account: '',
            avatar: '',
            nickname: '',
            power: [],
            role: [],
            token: ''
        }
    },
    actions: {
        login(userInfo: GuardType) {
            this.id = userInfo.id
            this.account = userInfo.account
            this.avatar = userInfo.avatar
            this.nickname = userInfo.nickname
            this.power = userInfo.power
            this.role = userInfo.role
            this.token = userInfo.token
        },

        logout() {
            this.id = 0
            this.account = ''
            this.avatar = ''
            this.nickname = ''
            this.power = []
            this.role = []
            this.token = ''
        }
    },

    getters: {
        /**
         * 游客
         */
        tourist(): boolean {
            return this.token.length === 0
        }
    },

    persist: {//持久化储存
        enabled: true,
        strategies: [
            {
                key: `${cache.prefix()}-store-guard`, // 默认key是上面store的id，可自定义key
                storage: localStorage,//本地储存驱动
                paths: ['token']
            }
        ]
    }
})